from django.db import models
from Doctor_django.utils.models import BaseModel
from stdimage import StdImageField
from django.utils.safestring import mark_safe


# 科室模型类
class DepartmentModel(BaseModel):
    DIRECTION_CHOICES = {
        (0, '常见科室'),
        (1, '内科'),
        (2, '外科'),
        (3, '其他'),
    }
    name = models.CharField(max_length=50, verbose_name='科室名称')
    direction = models.SmallIntegerField(choices=DIRECTION_CHOICES, default=3, verbose_name='科室方向')
    cover = StdImageField(upload_to='department/%Y/', null=True, blank=True, verbose_name='科室封面', variations={
                                       # 'large': (600, 400),
                                       'thumb_800x800': {'width': 800, 'height': 800},
                                       # 'medium': (300, 200),
                                       'thumb_400x400': {'width': 400, 'height': 400},
                                       # 'thumbnail': (100, 100, True),
                                       'thumb_50x50': {'width': 50, 'height': 50},
                                })

    class Meta:
        db_table = 'department_info'
        verbose_name_plural = '科室信息表'

    def __str__(self):
        return self.name

    def cover_small(self):
        # 小图展示
        return mark_safe(f'<img alt="" src="{self.cover.thumb_50x50.url}"></img>')

    cover.short_description = '科室封面'
    # 图片是否显示
    cover.allow_tags = True
    # 设置排序顺序
    cover.admin_order_field = "cover"


# 医院模型类
class HospitalModel(models.Model):
    LEVEL_CHOICES = {
        (0, '一级甲等'),
        (1, '二级甲等'),
        (2, '三级甲等'),
        (3, '一级乙等'),
        (4, '二级乙等'),
        (5, '三级乙等'),
        (6, '一级丙等'),
        (7, '二级丙等'),
        (8, '三级丙等'),
    }
    name = models.CharField(max_length=200, verbose_name='医院名称')
    tel = models.CharField(max_length=50, verbose_name='医院电话')
    address = models.CharField(max_length=255, verbose_name='医院地址')
    profile = models.TextField(null=True, blank=True, verbose_name='医院简介')
    level = models.SmallIntegerField(choices=LEVEL_CHOICES, default=0, verbose_name='医院等级')
    cover = StdImageField(upload_to='hospital/%Y/', null=True, blank=True, verbose_name='医院封面', variations={
                                       # 'large': (600, 400),
                                       'thumb_800x800': {'width': 800, 'height': 800},
                                       # 'medium': (300, 200),
                                       'thumb_400x400': {'width': 400, 'height': 400},
                                       # 'thumbnail': (100, 100, True),
                                       'thumb_50x50': {'width': 50, 'height': 50},
                                })
    department = models.ManyToManyField(to=DepartmentModel, verbose_name='医院科室')

    class Meta:
        db_table = 'hospital_info'
        verbose_name_plural = '医院信息表'

    def __str__(self):
        return self.name

    def cover_small(self):
        # 小图展示
        return mark_safe(f'<img alt="" src="{self.cover.thumb_50x50.url}"></img>')

    cover.short_description = '医院封面'
    # 图片是否显示
    cover.allow_tags = True
    # 设置排序顺序
    cover.admin_order_field = "cover"


# 疾病模型类
class IllnessModel(BaseModel):
    name = models.CharField(max_length=255, verbose_name='疾病名称')
    department = models.ForeignKey(to=DepartmentModel, related_name='illness_list', on_delete=models.DO_NOTHING, null=True, blank=True, db_constraint=False, verbose_name='科室')

    class Meta:
        db_table = 'illness_info'
        verbose_name_plural = '疾病信息表'

    def __str__(self):
        return self.name
